package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.DefenceArea;
import com.example.prison.service.DefenceAreaService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("简略防区列表")
@RestController
@RequestMapping("/api")
public class DefenceAreaController {

    @Autowired
    private DefenceAreaService defenceAreaService;

    @ApiOperation("获取简略防区列表")
    @GetMapping("/getDefenceArea")
    public Response<PageInfo<DefenceArea>> getArea(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取简略防区列表");
        PageInfo<DefenceArea> pageInfo = defenceAreaService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增防区")
    @PostMapping("/addDefenceArea")
    public Response<?> addDefenceArea(@RequestBody DefenceArea area) {
        log.info("新增防区");
        area.setStatus(0);
        int result = defenceAreaService.addDefenceArea(area);
        if (result == -1) {
            return Response.error("简略防区已存在！");
        } else if (result == 0) {
            return Response.error("防区已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("新增哨位")
    @PostMapping("/addDefenceAreaSentryPost")
    public Response<?> addSentryPost(@RequestBody DefenceArea area) {
        log.info("新增哨位");
        int result = defenceAreaService.addSentryPost(area);
        if (result == -1) {
            return Response.error("简略防区已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("依据id删除简略防区")
    @PostMapping("/deleteDefenceArea")
    public Response<?> deleteDefenceAreaById(@Parameter(description = "防区ID") @RequestParam(value = "ids") Integer[] ids) {
        log.info("删除简略防区");
        int result = defenceAreaService.deleteDefenceArea(ids);
        if (result == -1) {
            return Response.error("防区不存在！");
        } else if (result == 0) {
            return Response.error("防区列表中该防区段不存在！");
        }
        return Response.success("删除成功！");
    }

    @ApiOperation("依据id删除简略哨位")
    @PostMapping("/deleteSentryPost")
    public Response<?> deleteSentryPost(@Parameter(description = "哨位ID") @RequestParam(value = "ids") Integer[] ids) {
        log.info("删除简略哨位");
        int result = defenceAreaService.deleteSentryPost(ids);
        if (result == -1) {
            return Response.error("哨位不存在！");
        } else if (result == 0) {
            return Response.error("防区列表中哨位区域不存在！");
        }
        return Response.success("删除成功！");
    }

    @ApiOperation("编辑状态")
    @PostMapping("/updateAreaStatus")
    public Response<?> updateAreaStatus(@RequestBody DefenceArea area) {
        log.info("编辑状态");
        Integer id = area.getId();
        int result = defenceAreaService.updateAreaStatus(id,area);
        if (result == -1) {
            return Response.error("更新失败！防区已存在！");
        }  else if (result == 0) {
            return Response.error("简略防区列表中该区域不存在！");
        } else if (result == 2) {
            return Response.error("防区不存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据编号获取简略防区列表")
    @GetMapping("/getDefenceAreaByNumber")
    public Response<PageInfo<DefenceArea>> getDefenceAreaByNumber(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @Parameter(description = "编号")@RequestParam(value = "number") String number) {
        log.info("依据编号获取简略防区列表");
        PageInfo<DefenceArea> pageInfo = defenceAreaService.getByNumber(pageNumber, pageSize,number);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此防区不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("依据状态获取简略防区列表")
    @GetMapping("/getDefenceAreaByStatus")
    public Response<PageInfo<DefenceArea>> getDefenceAreaByStatus(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @Parameter(description = "状态")@RequestParam(value = "status") String status) {
        log.info("依据状态获取简略防区列表");
        PageInfo<DefenceArea> pageInfo = defenceAreaService.getByStatus(pageNumber, pageSize,status);
        if(ObjectUtils.isEmpty(pageInfo.getList())) {
            return Response.error("此防区不存在！");
        }
        return Response.success("获取成功！", pageInfo);
    }
}
